package cn.bluech.lib.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

import cn.bluech.lib.R;

/**
 * 可以指定纵横比的View
 * 在指定长或宽时自动根据比例适配宽或高
 * 纵横比通过aspect_ratio配置，默认为16:9
 *   例：app:aspect_ratio="16:9"
 * Description:
 * Create by Codeant on 2018/10/19.
 */
public class FixRatioImageView extends AppCompatImageView {
    private static final float ASPECT_RATIO_DEF = 16/9f;
    private float aspectRatio = ASPECT_RATIO_DEF; // 屏幕纵横比

    public FixRatioImageView(Context context) {
        super(context);
    }

    public FixRatioImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FixRatioImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.FixRatioImageView);
        String ratioStr = array.getString(R.styleable.FixRatioImageView_aspect_ratio);
        array.recycle();

        try {
            String[] subs = TextUtils.isEmpty(ratioStr)? new String[]{""}:ratioStr.split(":",2);
            if (subs.length==2){
                float ratioW = Float.parseFloat(subs[0]);
                float ratioH = Float.parseFloat(subs[1]);
                if((ratioW!=0)&&(ratioH!=0))
                    aspectRatio = ratioW/ratioH;
            }
        }catch (Exception e){
            aspectRatio = ASPECT_RATIO_DEF;
        }
    }

    public void setAspectRatio(float aspectRatio) {
        if(aspectRatio!=0) {
            this.aspectRatio = aspectRatio;
            requestLayout();
        }
    }

    /**
     * 如果宽度固定，高度自适应的情况下，调整为16：9的比例
     * MeasureSpec.AT_MOST 最大模式
     *   对应wrap_content，控件大小不受限制
     * MeasureSpec.EXACTLY 精确模式
     *   对应match_parent或者指定具体值，控件大小被指定
     * MeasureSpec.UNSPECIFIED
     *   对应父控件为AdapterView类型的控件，对此控件大小受不确定的，可以认为是AT_MOST
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        if((( heightMode==MeasureSpec.AT_MOST) ||(heightMode==MeasureSpec.UNSPECIFIED))
          &&( widthMode==MeasureSpec.EXACTLY)){ // WRAP_CONTENT || 父控件为AdapterView
            // 指定宽度未指定高度
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (widthSize/aspectRatio),MeasureSpec.EXACTLY);
        }else if ((( widthMode==MeasureSpec.AT_MOST) ||(widthMode==MeasureSpec.UNSPECIFIED))
                &&( heightMode==MeasureSpec.EXACTLY)){
            // 指定高度未指定宽度
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (heightSize*aspectRatio),MeasureSpec.EXACTLY);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
